﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BLL
{
    public class ePioneer_CourseInfo
    {
        private readonly DAL.ePioneer_CourseInfo dal = new DAL.ePioneer_CourseInfo();
        public ePioneer_CourseInfo()
        { }
        #region  Method

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_CourseInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_CourseInfo GetModel(int Id)
        {
            return dal.GetModel(Id);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.ePioneer_CourseInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.ePioneer_CourseInfo> DataTableToList(DataTable dt)
        {
            List<Model.ePioneer_CourseInfo> modelList = new List<Model.ePioneer_CourseInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.ePioneer_CourseInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.ePioneer_CourseInfo();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["Title"] != null && dt.Rows[n]["Title"].ToString() != "")
                    {
                        model.Title = dt.Rows[n]["Title"].ToString();
                    }
                    if (dt.Rows[n]["CourseType"] != null && dt.Rows[n]["CourseType"].ToString() != "")
                    {
                        model.CourseType = dt.Rows[n]["CourseType"].ToString();
                    }
                    if (dt.Rows[n]["SolutionType"] != null && dt.Rows[n]["SolutionType"].ToString() != "")
                    {
                        model.SolutionType = dt.Rows[n]["SolutionType"].ToString();
                    }
                    if (dt.Rows[n]["Lecturer"] != null && dt.Rows[n]["Lecturer"].ToString() != "")
                    {
                        model.Lecturer = int.Parse(dt.Rows[n]["Lecturer"].ToString());
                    }
                    if (dt.Rows[n]["MainContent"] != null && dt.Rows[n]["MainContent"].ToString() != "")
                    {
                        model.MainContent = dt.Rows[n]["MainContent"].ToString();
                    }
                    if (dt.Rows[n]["BriefInfo"] != null && dt.Rows[n]["BriefInfo"].ToString() != "")
                    {
                        model.BriefInfo = dt.Rows[n]["BriefInfo"].ToString();
                    }
                    if (dt.Rows[n]["IReservation"] != null && dt.Rows[n]["IReservation"].ToString() != "")
                    {
                        model.IReservation = int.Parse(dt.Rows[n]["IReservation"].ToString());
                    }
                    if (dt.Rows[n]["Attachment"] != null && dt.Rows[n]["Attachment"].ToString() != "")
                    {
                        model.Attachment = dt.Rows[n]["Attachment"].ToString();
                    }
                    if (dt.Rows[n]["AttachmentName"] != null && dt.Rows[n]["AttachmentName"].ToString() != "")
                    {
                        model.AttachmentName = dt.Rows[n]["AttachmentName"].ToString();
                    }
                    if (dt.Rows[n]["CourseStartDate"] != null && dt.Rows[n]["CourseStartDate"].ToString() != "")
                    {
                        model.CourseStartDate = DateTime.Parse(dt.Rows[n]["CourseStartDate"].ToString());
                    }
                    if (dt.Rows[n]["StartTime"] != null && dt.Rows[n]["StartTime"].ToString() != "")
                    {
                        model.StartTime = dt.Rows[n]["StartTime"].ToString();
                    }
                    if (dt.Rows[n]["EndTime"] != null && dt.Rows[n]["EndTime"].ToString() != "")
                    {
                        model.EndTime = dt.Rows[n]["EndTime"].ToString();
                    }
                    if (dt.Rows[n]["CreateDate"] != null && dt.Rows[n]["CreateDate"].ToString() != "")
                    {
                        model.CreateDate = DateTime.Parse(dt.Rows[n]["CreateDate"].ToString());
                    }
                    if (dt.Rows[n]["IsCheck"] != null && dt.Rows[n]["IsCheck"].ToString() != "")
                    {
                        model.IsCheck = int.Parse(dt.Rows[n]["IsCheck"].ToString());
                    }
                    if (dt.Rows[n]["ICount"] != null && dt.Rows[n]["ICount"].ToString() != "")
                    {
                        model.ICount = int.Parse(dt.Rows[n]["ICount"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method

        #region 自定义方法

        /// <summary>
        /// 增加下载数，返回0：成功
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string AddCount(int id)
        {
            Model.ePioneer_CourseInfo model = GetModel(id);
            if (model != null)
            {
                model.ICount = model.ICount + 1;
                if (Update(model))
                {
                    return "0";
                }
                else
                    return "1";
            }
            else
            {
                return "2";
            }
        }

        /// <summary>
        /// 获取全部信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllByCache()
        {
            string CacheKey = "ePioneer_CourseInfo";
            object objModel = LYM.CommonWeb.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetCourseInfo("", "", "", DateTime.Now.AddYears(-10), DateTime.Now.AddYears(10));
                    if (objModel != null)
                    {
                        int ModelCache = LYM.CommonWeb.ConfigHelper.GetConfigInt("ModelCache");
                        LYM.CommonWeb.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return objModel as DataTable;
        }

        /// <summary>
        /// 获取所有的课程信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAll()
        {
            return dal.GetCourseInfo("", "", "", DateTime.Now.AddYears(-10), DateTime.Now.AddYears(10));
        }

        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <param name="keyWord">关键字</param>
        /// <param name="courseType">课程类型</param>
        /// <param name="lecturer">讲师名</param>
        /// <param name="isLine">线上1或线下0</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public DataRow[] GetCourseInfo(string keyWord, string courseType, string lecturer, int isLine, DateTime startDate, DateTime endDate)
        {
            DataTable dt = GetAll();
            if (lecturer == "")
            {
                lecturer = "%";
            }
            if (courseType == "")
            {
                courseType = "%";
            }
            DataRow[] drs = dt.Select("IsLine='" + isLine + "' and Title like '%" + keyWord + "%' and CourseStartDate>='" + startDate + "' and CourseStartDate<='" + endDate + "' and Lecturer like '" + lecturer + "' and CourseType like '" + courseType + "'");
            return drs;
        }

        /// <summary>
        /// 根据Id获取课程信息
        /// </summary>
        /// <param name="id">课程ID</param>
        /// <param name="isLine">线上1或线下0</param>
        /// <returns></returns>
        public DataRow[] GetCourseInfo(int id)
        {
            DataTable dt = GetAll();
            DataRow[] drs = dt.Select("Id='" + id + "'");
            return drs;
        }

        /// <summary>
        /// 获取经典课程信息
        /// </summary>
        /// <returns></returns>
        public DataRow[] GetCourseInfo()
        {
            DataTable dt = GetAll();
            DataRow[] drs = dt.Select("IsLine='0' and IsTop='1'");
            return drs;
        }

        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <param name="lecturer">讲师名</param>
        /// <param name="location">地区</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns></returns>
        public DataRow[] GetCourseInfo(string lecturer, string location, DateTime startDate, DateTime endDate)
        {
            DataTable dt = GetAll();
            if (lecturer == "")
            {
                lecturer = "%";
            }
            if (location == "")
            {
                location = "%";
            }
            DataRow[] drs = dt.Select("IsLine='0' and Lecturer like '" + lecturer + "' and Location like '" + location + "' and CourseStartDate>='" + startDate + "' and CourseStartDate<'" + endDate + "'");
            return drs;
        }

        #endregion
    }
}
